<template>
	<div class="teacher-teacherinformationedit">
		<Spin size="large" fix v-if="spinShow"></Spin>
		<Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">


			<FormItem label="上传头像">
				<Upload :action="picurl" name="file" :headers="headers" :on-success="upload_success">
					<Button type="ghost" icon="ios-cloud-upload-outline">上传头像</Button>
				</Upload>
				<div class="rescources_files">
					<div class="demo-upload-list">
						<img v-if="formValidate.imgphotos !=null" :src="formValidate.imgphotos">
						<img v-else src="@/static/img/noImage_cn.png" alt="">
					</div>
				</div>
			</FormItem>

			<FormItem label="本人签字图片">
				<Upload :action="picurl" name="file" :headers="headers" :on-success="signimg_success">
					<Button type="ghost" icon="ios-cloud-upload-outline">上传图片</Button>
				</Upload>
				<div class="rescources_files">
					<div class="demo-upload-list">
						<img v-if="formValidate.signimg !=null" :src="formValidate.signimg">
						<img v-else src="@/static/img/noImage_cn.png" alt="">
					</div>
				</div>
			</FormItem>
			<FormItem prop="idcard">
				<Input v-model="formValidate.idcard" placeholder="身份证号" style="width:80%">
				<span slot="prepend">身份证号</span>
				</Input>
			</FormItem>
			<FormItem prop="teacher_name.zh">
				<Input v-model="formValidate.teacher_name.zh" placeholder="姓名" style="width:80%">
				<span slot="prepend">姓名</span>
				</Input>
			</FormItem>

			<FormItem prop="email">
				<Input v-model="formValidate.email" placeholder="电子邮箱" style="width:80%">
				<span slot="prepend">电子邮箱</span>
				</Input>
			</FormItem>

			<FormItem prop="telnumber">

				<Input v-model="formValidate.telnumber" placeholder="电子邮箱" style="width:80%">
				<span slot="prepend">手机号码</span>
				</Input>
			</FormItem>

			<FormItem label="性别" prop="gender">
				<Select v-model="formValidate.gender" style="width:80%" placeholder="请选择">
					<Option v-for="item in formItem.gender" :value="item.dictionary_id" :key="item.dictionary_id">{{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}</Option>
				</Select>
			</FormItem>

			<FormItem label="国籍" prop="nationality">
				<Select v-model="formValidate.nationality" style="width:80%">
					<Option v-for="item in formItem.nationality" :value="item.dictionary_id" :key="item.value">{{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}</Option>
				</Select>
			</FormItem>

			<FormItem label="民族" prop="nation">
				<Select v-model="formValidate.nation" style="width:80%">
					<Option v-for="item in formItem.Nation" :value="item.dictionary_id" :key="item.value">
					{{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}
					</Option>
				</Select>
			</FormItem>

			<FormItem label="政治面貌" prop="politicaltype">
				<Select v-model="formValidate.politicaltype" style="width:80%">
					<Option v-for="item in formItem.politicaltype" :value="item.dictionary_id" :key="item.value">{{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}</Option>
				</Select>
			</FormItem>

			<FormItem label="在岗状态">
				<Select v-model="formValidate.workstatus" style="width:80%">
					<Option v-for="item in formItem.workstatus" :value="item.dictionary_id" :key="item.value">
					{{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}
					</Option>
				</Select>
			</FormItem>

			<FormItem label="人才类型">
				<Select v-model="formValidate.type" style="width:80%">
					<Option v-for="item in formItem.type" :value="item.dictionary_id" :key="item.value">
					{{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}
					</Option>
				</Select>
			</FormItem>

			<FormItem label="最高职称">
				<Select v-model="formValidate.jobtitle" style="width:80%">
					<Option v-for="item in formItem.jobtitle" :value="item.dictionary_id" :key="item.value">
					{{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}
					</Option>
				</Select>
			</FormItem>

			<FormItem label="最高学历">
				<Select v-model="formValidate.education" style="width:80%">
					<Option v-for="item in formItem.education" :value="item.dictionary_id" :key="item.value">
					{{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}
					</Option>
				</Select>
			</FormItem>

			<FormItem label="最高学位">
				<Select v-model="formValidate.degree" style="width:80%">
					<Option v-for="item in formItem.degree" :value="item.dictionary_id" :key="item.value">
					{{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}
					</Option>
				</Select>
			</FormItem>

			<FormItem label="是否硕导">
				<Select v-model="formValidate.mastersupervisor" style="width:80%">
					<Option v-for="item in formItem.mastersupervisor" :value="item.dictionary_id" :key="item.value">
					{{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}
					</Option>
				</Select>
			</FormItem>

			<FormItem label="是否博导">
				<Select v-model="formValidate.teach" style="width:80%">
					<Option v-for="item in formItem.teach" :value="item.dictionary_id" :key="item.value">
					{{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}
					</Option>
				</Select>
			</FormItem>

			<FormItem label="出生日期" prop="birthday">
				<DatePicker type="datetime" v-model="formValidate.birthday" placeholder="请选择出生日期" ></DatePicker>
			</FormItem>

			<FormItem label="参加工作时间" prop="workdate">
				<DatePicker type="datetime" v-model="formValidate.workdate" placeholder="请选择参加工作时间" ></DatePicker>
			</FormItem>

			<FormItem label="人才类别(按六支队伍分)" style="width:100%;">
				<CheckboxGroup v-model="formValidate.talentcategory_six_id">
					<Checkbox v-for="(val,index) in formItem.talentcategory_six" :label="val.dictionary_id" :key="index">
						{{val.dictionary_name.zh?val.dictionary_name.zh:val.dictionary_name.mn}}
					</Checkbox>
				</CheckboxGroup>
			</FormItem>

			<FormItem label="人才类别(按九支队伍分)" style="width:100%;">
				<CheckboxGroup v-model="formValidate.talentcategory_nine_id">
					<Checkbox v-for="(val,index) in formItem.talentcategory_nine" :label="val.dictionary_id" :key="index">
						{{val.dictionary_name.zh?val.dictionary_name.zh:val.dictionary_name.mn}}
					</Checkbox>
				</CheckboxGroup>
			</FormItem>
			<FormItem label="籍贯" style="width:100%;">
				<Select v-model="formValidate.nativeprovince" style="width:30%" @on-change="selectNative">
					<Option v-for="item in regionList" v-if="item.parent_id ==0" :value="item.id" :key="item.value">{{item.region_name.zh?item.region_name.zh:item.region_name.mn}}</Option>
				</Select>
				<Select v-model="formValidate.nativecity" style="width:30%" @on-change="selectCity">
					<Option v-for="item in nativeCity" :value="item.id" :key="item.value">{{item.region_name.zh?item.region_name.zh:item.region_name.mn}}</Option>
				</Select>
				<Select v-model="formValidate.nativecounty" style="width:30%">
					<Option v-for="item in nativeCounty" :value="item.id" :key="item.value">{{item.region_name.zh?item.region_name.zh:item.region_name.mn}}</Option>
				</Select>
			</FormItem>

			<FormItem label="出生地" style="width:100%;">
				<Select v-model="formValidate.birthprovince" style="width:30%" @on-change="selectProvince">
					<Option v-for="item in regionList" v-if="item.parent_id ==0" :value="item.id" :key="item.value">{{item.region_name.zh?item.region_name.zh:item.region_name.mn}}</Option>
				</Select>
				<Select v-model="formValidate.birthcity" style="width:30%" @on-change="selectBirthcity">
					<Option v-for="item in birthCity" :value="item.id" :key="item.value">{{item.region_name.zh?item.region_name.zh:item.region_name.mn}}</Option>
				</Select>
				<Select v-model="formValidate.birthcounty" style="width:30%">
					<Option v-for="item in birthCounty" :value="item.id" :key="item.value">{{item.region_name.zh?item.region_name.zh:item.region_name.mn}}</Option>
				</Select>
			</FormItem>

			<FormItem label="现住址" style="width:100%;">
				<Select v-model="formValidate.cmbProvince3" style="width:30%" @on-change="cmbProvince">
					<Option v-for="item in regionList" v-if="item.parent_id ==0" :value="item.id" :key="item.value">{{item.region_name.zh?item.region_name.zh:item.region_name.mn}}</Option>
				</Select>
				<Select v-model="formValidate.cmbcity3" style="width:30%" @on-change="selectCmbCounty">
					<Option v-for="item in cmbCity" :value="item.id" :key="item.value">{{item.region_name.zh?item.region_name.zh:item.region_name.mn}}</Option>
				</Select>
				<Select v-model="formValidate.cmbcounty3" style="width:30%">
					<Option v-for="item in cmbCounty" :value="item.id" :key="item.value">{{item.region_name.zh?item.region_name.zh:item.region_name.mn}}</Option>
				</Select>
			</FormItem>

			<FormItem label="户籍所在地" style="width:100%;">
				<Select v-model="formValidate.cmbProvince2" style="width:30%" @on-change="selectNat">
					<Option v-for="item in regionList" v-if="item.parent_id ==0" :value="item.id" :key="item.value">{{item.region_name.zh?item.region_name.zh:item.region_name.mn}}</Option>
				</Select>
				<Select v-model="formValidate.cmbcity2" style="width:30%" @on-change="selectNatCity">
					<Option v-for="item in cmbNation" :value="item.id" :key="item.value">{{item.region_name.zh?item.region_name.zh:item.region_name.mn}}</Option>
				</Select>
				<Select v-model="formValidate.cmbcounty2" style="width:30%">
					<Option v-for="item in cmbNatCity" :value="item.id" :key="item.value">{{item.region_name.zh?item.region_name.zh:item.region_name.mn}}</Option>
				</Select>
			</FormItem>

			<FormItem prop="introduction.zh">
				<Input v-model="formValidate.introduction.zh" type="textarea" :autosize="{minRows: 4,maxRows: 4}" placeholder="个人介绍" style="width:80%">
				<span slot="prepend">个人介绍</span>
				</Input>
			</FormItem>

			<FormItem>
				<Button type="primary" @click="handleSubmit('formValidate')" :loading="loadingbtn">提交</Button>
				<Button type="ghost" style="margin-left: 8px" @click="handleReset('formValidate')">重置</Button>
			</FormItem>
			<div style="clear:both"></div>
		</Form>
		<information-extend v-if="look" :teacher_id="teacher_id" :user_id="user_id" :school_id="school_id"></information-extend>
	</div>
</template>

<script>
	export default {
		props: ['teacher_id', 'school_id'],
		components: {
			InformationExtend: resolve => {
				require(['./InformationExtend.vue'], resolve);
			},
		},
		data() {
			return {
				look: false,
				user_id: null,
				loadingbtn:false,
				spinShow:false,
				// which_to_show:'InformationExtend',
				formValidate: {
					teacher_name: {
						zh: '',
						mn: '',
						en: '',
					},
					introduction: {
						zh: '',
						mn: '',
						en: '',
					},
					idcard: '',
					email: '',
					telnumber: null,
					type: '',
					gender: '',
					nation: '',
					birthday: '',
					nationality: '',
					politicaltype: '',
					education: '',
					degree: '',
					mastersupervisor: '',
					teach: '',
					talentcategory_six_id: [],
					talentcategory_nine_id: [],
					workstatus: '',
					jobtitle: '',
					workdate: '',
					unitname: '',
					nativeprovince: '',
					nativecity: '',
					nativecounty: '',
					birthprovince: '',
					birthcity: '',
					birthcounty: '',
					cmbProvince3: '',
					cmbcity3: '',
					cmbcounty3: '',
					cmbProvince2: '',
					cmbcity2: '',
					cmbcounty2: '',
					imgphotos: '',
					signimg: ''
				},
				formItem: [],
				teacheredit: [],
				regionList: [],
				nativeCity: [],
				nativeCounty: [],
				birthCity: [],
				birthCounty: [],
				cmbCity: [],
				cmbCounty: [],
				cmbNation: [],
				cmbNatCity: [],
				picurl: this.$project.axios.baseUrl + '/api/user/v1/upload',
				headers: {
                    'Authorization': 'Bearer '+(localStorage[this.$project.axios.Authorization] || '')
                },
				ruleValidate: {
					'teacher_name.zh': [{
							required: true,
							message: '汉文姓名不能为空',
							trigger: 'blur'
						},

					],
					// idcard: [
					//   {required:true,pattern:/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/,message:'请输入正确的身份号',trigger:'blur'}
					// ],
					idcard: [{
						required: true,
						message: '请输入正确的身份号',
						trigger: 'blur'
					}],
					// email: [
					//   {required: true, message: '邮箱不能为空', trigger: 'blur'},
					//   {type: 'email', message: '邮箱格式不正确', trigger: 'blur'}
					// ],
					// telnumber: [
					//   {type: 'number',required:true,message:'请输入正确的手机号',trigger:'blur'}
					// ],
					// gender: [
					//   { type:'number',required: true, message: '请选择性别', trigger: 'change' }
					// ],
					// nation: [
					//   { type:'number',required: true, message: '请选择民族', trigger: 'change' }
					// ],
					// nationality: [
					//   { type:'number',required: true, message: '请选择国籍', trigger: 'change' }
					// ],
					// unitname: [
					//   { type:'number',required: true, message: '请选择学校', trigger: 'change' }
					// ],
					// workstatus: [
					//   { type:'number',required: true, message: '请选择政治面貌', trigger: 'change' }
					// ],
				},
			}
		},
		// created() {
		// },
		methods: {

			selectNative(val) {
				this.$http.get('api/base/v1/get/region/list?pid=' + val).then(item => {
					this.nativeCity = item.data
				})
			},
			selectCity(val) {
				this.$http.get('api/base/v1/get/region/list?pid=' + val).then(item => {
					this.nativeCounty = item.data
				})
			},
			selectProvince(val) {
				this.$http.get('api/base/v1/get/region/list?pid=' + val).then(item => {
					this.birthCity = item.data
				})
			},
			selectBirthcity(val) {
				this.$http.get('api/base/v1/get/region/list?pid=' + val).then(item => {
					this.birthCounty = item.data
				})
			},
			cmbProvince(val) {
				this.$http.get('api/base/v1/get/region/list?pid=' + val).then(item => {
					this.cmbCity = item.data
				})
			},
			selectCmbCounty(val) {
				this.$http.get('api/base/v1/get/region/list?pid=' + val).then(item => {
					this.cmbCounty = item.data
				})
			},
			selectNat(val) {
				this.$http.get('api/base/v1/get/region/list?pid=' + val).then(item => {
					this.cmbNation = item.data
				})
			},
			selectNatCity(val) {
				this.$http.get('api/base/v1/get/region/list?pid=' + val).then(item => {
					this.cmbNatCity = item.data
				})
			},
			upload_success(str) {
				if (str != null) {
					this.formValidate.imgphotos = str.data.file_url
				}

			},
			signimg_success(str) {
				if (str != null) {
					this.formValidate.signimg = str.data.id
				}
			},

			handleSubmit(name) {
				this.loadingbtn = true
				this.$refs[name].validate((valid) => {
					if (valid) {
						this.$http.post('api/teacher/v1/teacher/editupdate/' + this.teacher_id, this.formValidate).then(val => {
							this.$Message.success(this.$t('edit_success'));
							this.$emit('requery', true);
							this.loadingbtn = false
						}).catch(err => {
							this.$Message.error(this.$t('edit_error'))
							this.loadingbtn = false
						});
					} else {
						this.$Message.error(this.$t('Please_fill_in_the_complete'));
						this.loadingbtn = false
					}
				})
			},
			handleReset(name) {
				this.$refs[name].resetFields();
			}
		},
		created() {
			this.spinShow = true;
			this.$http.get('api/dictionary/v1/dictionary/pidquerys?gender&type&nationality&politicaltype&workstatus&jobtitle&education&degree&mastersupervisor&teach&talentcategory_six&talentcategory_nine&Nation&type').then((response) => {

				this.formItem = response.data
			});
			this.$http.get('api/base/v1/get/region/list').then(val => {

				this.regionList = val.data
			})
			this.$http.get('api/teacher/v1/teacher/find/' + this.teacher_id).then(val => {
				let res = val.data
				this.user_id = res.user_id
				this.teacheredit = res
				this.formValidate.idcard = res.idcard_org
				this.formValidate.teacher_name = res.teacher_name
				this.formValidate.introduction = res.introduction !=null?res.introduction:{
					zh:null,mn:null,en:null
				}
				this.formValidate.email = res.email
				this.formValidate.telnumber = res.telnumbe == '' ? 0 : parseInt(res.telnumber)
				this.formValidate.gender = parseInt(res.gender)

				this.formValidate.nation = parseInt(res.nation)
				this.formValidate.nationality = parseInt(res.nationality)
				this.formValidate.politicaltype = parseInt(res.politicaltype)
				this.formValidate.education = parseInt(res.education)
				this.formValidate.degree = parseInt(res.degree)
				this.formValidate.mastersupervisor = parseInt(res.mastersupervisor)
				this.formValidate.teach = parseInt(res.teach)
				this.formValidate.workstatus = parseInt(res.workstatus)
				this.formValidate.jobtitle = parseInt(res.jobtitle)
				this.formValidate.type = parseInt(res.type)
				this.formValidate.birthday = (res.birthday)
				this.formValidate.workdate = (res.workdate)

				this.formValidate.imgphotos = res.imgphotos
				this.formValidate.signimg = res.signimg

				this.formValidate.unitname = (res.unitname)
				if (res.nativeprovince != null) {
					this.formValidate.nativeprovince = parseInt(res.nativeprovince)
					this.selectNative(this.formValidate.nativeprovince)
				}
				if (res.nativecity != null) {
					this.formValidate.nativecity = parseInt(res.nativecity)
					this.selectCity(this.formValidate.nativecity)
				}
				if (res.nativecounty != null) {
					this.formValidate.nativecounty = parseInt(res.nativecounty)
				}
				if (res.birthprovince != null) {
					this.formValidate.birthprovince = parseInt(res.birthprovince)
					this.selectProvince(this.formValidate.birthprovince)
				}
				if (res.birthcity != null) {
					this.formValidate.birthcity = parseInt(res.birthcity)
					this.selectBirthcity(this.formValidate.birthcity)
				}
				if (res.birthcounty != null) {
					this.formValidate.birthcounty = parseInt(res.birthcounty)
				}
				if (res.cmbProvince3 != null) {
					this.formValidate.cmbProvince3 = parseInt(res.cmbProvince3)
					this.cmbProvince(this.formValidate.cmbProvince3)
				}
				if (res.cmbcity3 != null) {
					this.formValidate.cmbcity3 = parseInt(res.cmbcity3)
					this.selectCmbCounty(this.formValidate.cmbcity3)
				}
				if (res.cmbcounty3 != null) {
					this.formValidate.cmbcounty3 = parseInt(res.cmbcounty3)
				}
				if (res.cmbProvince2 != null) {
					this.formValidate.cmbProvince2 = parseInt(res.cmbProvince2)
					this.selectNat(this.formValidate.cmbProvince2)
				}
				if (res.cmbcity2 != null) {
					this.formValidate.cmbcity2 = parseInt(res.cmbcity2)
					this.selectNatCity(this.formValidate.cmbcity2)
				}
				if (res.cmbcounty2 != null) {
					this.formValidate.cmbcounty2 = parseInt(res.cmbcounty2)
				}
				for (let item in res.talentcategory_nine) {

					this.formValidate.talentcategory_nine_id.push(parseInt(res.talentcategory_nine[item]))
				}
				// console.log(res)
				for (let item in res.talentcategory_six) {
					this.formValidate.talentcategory_six_id.push(parseInt(res.talentcategory_six[item]))
				}
				this.look = true
				this.spinShow = false
			})
		}
	}
</script>

<style lang="less">
	.teacher-teacherinformationedit {
		.rescources_files {
			.demo-upload-list {
				display: inline-block;
				width: 80px;
				height: 80px;
				text-align: center;
				line-height: 80px;
				border: 1px solid transparent;
				border-radius: 4px;
				overflow: hidden;
				background: #fff;
				position: relative;
				box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
				margin-right: 4px;
				img {
					width: 100%;
					height: 100%;
				}
				.demo-upload-list-cover {
					display: none;
					position: absolute;
					top: 0;
					bottom: 0;
					left: 0;
					right: 0;
					background: rgba(0, 0, 0, 0.6);
					i{
						color: #fff;
						font-size: 20px;
						cursor: pointer;
						margin: 0 2px;
					}
				}
				&:hover {

						display: block;

				}
			}
		}
		.ivu-form-item {
			width: 50%;
			float: left;
		}
	}
</style>


